<template>
	<view class="page-content" >
		<u-loading-page :loading="true" :loading-text="$t('pay.order-num')+'：'+orderNum"></u-loading-page>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				payType:null,
				url:'',
				orderNum:'',
				timer:'', // 定时器
				maxCheckNum:10,
				checkNum:1,
			}
		},
		onLoad(option){
			if(!option.order_num){
				this.$util.showToast(this.$t('pay.no-wait-pay'),function(){
					uni.navigateBack();
				})
			}else{
				this.orderNum = option.order_num;
			}
			
			// #ifdef H5
			if(option.jump == 'app'){
				// 跳转回APP
				window.location.href = "hbuilder://";
			}
			// #endif
			
			const thos = this;
			const eventChannel = this.getOpenerEventChannel();
			eventChannel.on('acceptDataFromOpenerPage', function(data) {
				if(data.url){
					thos.url = data.url;
					// #ifdef H5
					if(uni.$u.os() == 'ios'){
						window.location.href = data.url;
					}else{
						window.open(data.url,'_blank');
					}
					// #endif
					// #ifdef APP-PLUS
					// app使用webview
					thos.createWvAndLoadUrl();
					// #endif
					
					// window.open(data.url,'_blank');
					// plus.runtime.openURL(data.url);
				}else{
					uni.navigateBack();
				}
			});
/* 			eventChannel.on('acceptDataFromOpenerPage', function(data) {
			    if(data.url){
					_this.url = data.url;
					// #ifdef H5
						console.log(uni.$u.os());
						if(uni.$u.os() == 'ios'){
							window.location.href = data.url;
						}else{
							window.open(data.url,'_blank');
						}
						
					// #endif
					// #ifdef APP-PLUS
						_this.createWvAndLoadUrl();
					// #endif
				}
			}) */
		},
		onShow(){
			// 检测订单是否支付
			this.checkOrder();
		},
		onHide(){
			// 页面卸载 取消定时器
			clearInterval(this.timer);
			uni.hideLoading();
		},
		onUnload(){
			// 页面卸载 取消定时器
			clearInterval(this.timer);
			uni.hideLoading();
		},
		methods: {
			// 建立webview
			createWvAndLoadUrl(){
				// console.log(url);
				const platform = uni.getSystemInfoSync().platform
				let height = 0;
				let statusbar = 0;
				uni.getSystemInfo({
					success(sysInfo) {
						statusbar = sysInfo.statusBarHeight;
						height	= sysInfo.windowHeight;
					}
				})
				const wv = plus.webview.create('',"pay-webview");
				switch(platform){
				  case 'android':
				    wv.loadURL(this.url,{'Referer':'http://w25112319k.wicp.vip'});
				    break;
				  case 'ios':
				    wv.loadURL(this.url,{'Referer':'w25112319k.wicp.vip://'});
				    break;
				  default:
				    break;
				}
				// var wv = plus.webview.create(this.url, "pay-webview",{
				// 	 top:statusbar,
				// 	 height:height - statusbar
				// },{
				// 	'kernel': 'WKWebview',       //或者'WKWebview'
				// });
				wv.show();
				var currentWebview = this.$mp.page.$getAppWebview(); //获取当前页面的webview对象
				currentWebview.append(wv); //一定要append到当前的页面里！！！才能跟随当前页面一起做动画，一起关闭
			},
			
			// 检测支付订单
			async checkOrder(){
				const thos = this;
				
				this.timer = setInterval(async () => {
					var res = await thos.$util.getPayOrder(thos.orderNum);
					console.log('第'+thos.checkNum+'次检测支付订单'+thos.orderNum);
					if(res.code == 200 && res.data.order_state == 2){
						// 获取用户信息需要修改
						thos.Common.getUserInfo(true);
						clearInterval(thos.timer);
						console.log(getCurrentPages());
						thos.$util.showToast(this.$t('pay.pay-success'),async function(){
							uni.navigateBack();
						});
					}
					thos.loading = false;
					thos.checkNum++;
					if(thos.checkNum >= thos.maxCheckNum){
						clearInterval(thos.timer);
					}
				}, 3000);

			},
			
			// 弹窗
			
		}
	}
</script>

<style lang="scss">
    .status_bar{
        height: var(--status-bar-height);
        width: 100%;
    }
</style>
